import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/enums/base_file_type.dart';

class BaseFilePickModel {
  /// 0 图片  1 视频
  BaseFileType type;
  String fileUrl;
  String remark;

  BaseFilePickModel({@required this.fileUrl, this.remark, this.type});

  factory BaseFilePickModel.formJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) return null;
    int type = jsonRes['type'];
    return BaseFilePickModel(
      fileUrl: asT<String>(jsonRes['fileUrl']),
      remark: asT<String>(jsonRes['remark']),
      type: type == 0 ? BaseFileType.image : BaseFileType.video,
    );
  }

  toJson() {
    return <String, dynamic>{
      'fileUrl': fileUrl,
      'remark': remark,
      'type': checkType(),
    };
  }

  int checkType() {
    switch (type) {
      case BaseFileType.image:
        return 0;
      case BaseFileType.video:
        return 1;
    }
    return null;
  }
}

class BaseFilePick extends StatefulWidget {
  final List<BaseFilePickModel> dataList;

  const BaseFilePick({Key key, @required this.dataList}) : super(key: key);

  @override
  _BaseFilePickState createState() => _BaseFilePickState();
}

class _BaseFilePickState extends State<BaseFilePick> {
  List<BaseFilePickModel> get dataList => widget.dataList;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ...dataList.asMap().keys.map((e) => _buildItem(e)).toList(),
        BaseImageAddButton(onTap: () {
          _getFile(BaseFileType.image);
        }),
      ],
    );
  }

  /// 图片item
  Widget _buildItem(int index) {
    BaseFilePickModel model = dataList[index];
    return Container(
      margin: EdgeInsets.only(bottom: 12),
      height: 64,
      child: Row(
        children: [
          BaseImageComponent(width: 72, height: 72, imageUrl: model.fileUrl, fit: BoxFit.cover, hero: index.toString(), circular: 4),
          SizedBox(width: 12),
          Expanded(
            child: Container(
              height: 72,
              padding: EdgeInsets.only(left: 8.0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3),
                border: Border.all(color: BaseBodyTheme.greyBackgroundColor, width: 1),
              ),
              child: BaseTextArea(
                value: model.remark,
                maxLines: 3,
                isCount: false,
                onChanged: (v) => {dataList[index].remark = v},
              ),
            ),
          ),
          SizedBox(width: 12),
          Container(
            child: BaseCloseButton(
              onPress: () {
                setState(() {
                  dataList.removeAt(index);
                });
              },
            ),
          )
        ],
      ),
    );
  }

  void _getFile(BaseFileType type) async {
    BaseComponentUtils.openImageVideoPicker(context, (List<dynamic> assets) {
      List<dynamic> images = assets.map((e) => {'image': e, 'description': ''}).toList();
      List<BaseFilePickModel> _list = images.map((e) => BaseFilePickModel(fileUrl: e['image'], remark: '', type: type)).toList();
      setState(() {
        dataList.addAll(_list);
      });
    }, showVideoPicker: false);
  }
}
